package Domain.forumProtcol;

import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;

import objects.User;
import Domain.forumManager.ForumController;
import Domain.stompProtocol.ForumFrame;
import Domain.stompProtocol.StompClient;
import Domain.stompProtocol.StompFrame;
import Logger.ForumLogger;

public class AdderProtocol extends ProtocolHelper
{
	public AdderProtocol(StompClient inStompClient,ForumController f,ForumFrame frmFrame)
	{
		this._stompClient = inStompClient;
		this.forumController=f;
		forumfrm=frmFrame;
	}

	public String processMessage(StompFrame msg)
	{
		String ans="";
		String str = msg.getData();
		String nameOfUser = msg.getUser();

		if (msg.getData().startsWith("add user"))
		{
			forumController.createUser(nameOfUser);
			ans+=this.sendSecessMessage(nameOfUser) ;

		}
		else if (msg.getData().startsWith("add manager"))
		{
			forumController.createUser(nameOfUser);
			if(this.forumController.changeUserType(nameOfUser, 1)==1)
				ans +=this.sendSecessMessage(nameOfUser);
			else 
			{
				this.forumfrm.setErr(this.forumfrm.UNKOWNERR);
				ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
			}
		}
		else if (msg.getData().startsWith("remove user"))
		{
			int flag=1;
			if (nameOfUser != null)
			{
				String userToRemove = getUserToRemove(str).trim();
				int typeOfUser = this.forumController.getTypeOfUser(nameOfUser);
				if (typeOfUser==0) //default user
				{

					if(!nameOfUser.trim().equals(userToRemove)) 
					{
						forumfrm.setErr(forumfrm.ERRInvalidTypeOfUSer);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						flag=0;
					}

				}
				if (flag==1) 
				{
					int t = this.forumController.removeUser(userToRemove);
					if (t == 1)
					{
						ans+=this.sendSecessMessage(nameOfUser) ;
					}
					else {
						forumfrm.setErr(forumfrm.ERRUserNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
					}
				}
			}
		}
		//add message and send notifiction to all the freind of the user 
		//if user doesn't found won't do nothing 
		// if message doesn't not save well send an error
		else if (msg.getData().startsWith("add message"))
		{

			System.out.println("Content Of Message : " + getContentsOfMessage(str));
			//if we want to print the contact of the message


			try 
			{ //for the case one of them null
				//System.out.println("str is:\n"+str +"**\n");
				int tmp = getContentsOfMessage(str).lastIndexOf('\n');
				String title = getContentsOfMessage(str).substring(tmp).trim();

				Vector<User> friends = this.forumController.addMessage(getNameOfForum(str), nameOfUser, getNameOfThread(str),getContentsOfMessage(str));
				if (friends == null){
					this.forumfrm.setErr(ForumFrame.MessageNameExist);
					ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
				}
				else{

					ans = sendToFriendAndUsers(str, nameOfUser, ans,title);

				}
			} 
			catch (RuntimeException e) 
			{
				ans+=this.InvalidMessage(nameOfUser);
			}

		}
		else if (msg.getData().startsWith("remove message"))
		{

			try 
			{ //for the case one of them null
				int type = this.forumController.removeMessage(getNameOfForum(str), nameOfUser, getNameOfThread(str),getTitleOfMessage(str));
				if (type==1){

					ans+=this.sendSecessMessage(nameOfUser);
				}
				else{
					this.forumfrm.setErr(type);
					ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
				}
				/*switch (type)
				{ 
					case 1:ans+=this.sendSecessMessage(nameOfUser);
					//System.out.println("1 ++++++++++++++ here");
					break;
					case -1:
						this.forumfrm.setErr(this.forumfrm.ERRMsseageNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-1 ++++++++++++++ here");
					break;
					case -2 :
						//System.out.println("-2 ++++++++++++++ here");
						this.forumfrm.setErr(this.forumfrm.ERRThreadNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
					break;
					case -3 : 
						this.forumfrm.setErr(this.forumfrm.ERRForumNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-3 ++++++++++++++ here");
					break;
					case -4: 
						this.forumfrm.setErr(this.forumfrm.ERRUserNotFound);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-4 ++++++++++++++ here");
					break;
					case -5:
						this.forumfrm.setErr(this.forumfrm.ERRInvalidTypeOfUSer);
						ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
						//System.out.println("-5 ++++++++++++++ here");
					break;

				}*/
			} 
			catch (RuntimeException e) 
			{
				e.printStackTrace();
				ans+=this.InvalidMessage(nameOfUser);
				System.out.println("CATCH ++++++++++++++ here");
			}

		}
		else if (msg.getData().startsWith("add thread"))
		{
			if (nameOfUser != "")
			{
				try
				{
					int t = this.forumController.addThread(getNameOfForum(str), nameOfUser, getNameOfThread(str));
					switch (t)
					{
						case 1:ans+=this.sendSecessMessage(nameOfUser);
						break;
						case -1: 
							this.forumfrm.setErr(this.forumfrm.ERRUserNotFound);
							ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
							break;
						case -2 : 
							this.forumfrm.setErr(this.forumfrm.ERRInvalidTypeOfUSer);
							ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
							break;
						case -3 : 
							this.forumfrm.setErr(this.forumfrm.ERRForumNotFound);
							ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
							break;
						case ForumFrame.ThreadNameExist: 
							this.forumfrm.setErr(this.forumfrm.ThreadNameExist);
							ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
							break;
					}
				}
				catch (RuntimeException e) 
				{
					ans+=this.InvalidMessage(nameOfUser);
				}
			}
			else
			{
				ForumLogger.write(Level.INFO,"got inviled format of message");
				//System.out.println(" inviled format of message\n");
			}
		}


		else if (msg.getData().startsWith("add forum"))
		{
			int commandStatus = this.forumController.addForum(getNameOfForum(str), nameOfUser);
			if( commandStatus < 0)
			{
				this.forumfrm.setErr(commandStatus);
				ans+=this.sendPlayerMsg(nameOfUser,forumfrm);
			}		

			else ans += this.sendSecessMessage(nameOfUser);
		}

		else if (msg.getData().startsWith("add friend"))
		{
			try
			{
				//I have used get name of forum becouse he return the second argument that
				//in this situation is the other friend
				int x = this.forumController.addFreind(nameOfUser, getNameOfForum(str));
				if (x == -1){
					this.forumfrm.setErrUserNotFound(getNameOfForum(str));
					ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
				}
				else 
					ans += sendSecessMessage(nameOfUser);
			}
			catch (RuntimeException e) 
			{
				this.InvalidMessage(nameOfUser);
			}
		}
		else if (msg.getData().startsWith("remove friend"))
		{
			try
			{
				//I have used get name of forum becouse he return the second argument that
				//in this situation is the other friend
				int x = this.forumController.removeFriend(nameOfUser, getNameOfForum(str));
				switch (x)
				{
					case 1: ans += sendSecessMessage(nameOfUser);
					break;
					case -1:
						this.forumfrm.setErrUserNotFound(nameOfUser);
						ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
						break;
					case -2 :
						this.forumfrm.setErr(forumfrm.ERRUserNotYourFriend);
						//you need to implement it becouse this not sappose to appen
						ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);

						//:ans += this.sendPlayerMsg(nameOfUser, "the user  "+getNameOfForum(str)+" has not a friend of you");
						break;
					default :
						this.forumfrm.setErr(this.forumfrm.UNKOWNERR);
						ans += this.sendPlayerMsg(nameOfUser, this.forumfrm);
						break;
				}
			}
			catch (RuntimeException e) 
			{
				ForumLogger.write(Level.INFO,"the user: "+nameOfUser+" sent Invalid message");
				this.InvalidMessage(nameOfUser);
			}
		}
		//System.out.println("HERE");
		return ans;
	}
}
